import uuid

from selenium import webdriver;
from selenium.webdriver import ActionChains
import time;
import requests
from selenium.webdriver.support.select import Select

print(time.localtime(time.time() + 60 * 60 * 24 * 90))
print(uuid.uuid4())
MACHINE_CONFIG = [{
    "OPERATION": "WINDOWS",
    ##"MAC_ADDRESS": "B4:96:91:61:D3:FA;00:15:5D:2D:10:06;DC:71:96:11:93:33;DC:71:96:11:93:34;DE:71:96:11:93:33;E4:54:E8:8D:40:8A;00:50:56:C0:00:01;00:50:56:C0:00:08;DC:71:96:11:93:37"
    ##129
    "MAC_ADDRESS":"B4:96:91:61:D3:FA;00:15:5D:2D:10:06;DC:71:96:11:93:33;DC:71:96:11:93:34;DE:71:96:11:93:33;E4:54:E8:8D:40:8A;00:50:56:C0:00:01;00:50:56:C0:00:08;DC:71:96:11:93:37"
}]
dr = webdriver.Chrome();
dr.maximize_window();
baseUrl = "http://eplat.baosight.com/eplat/login.jsp"
# url="{}login.jsp".format(baseUrl);
dr.get(baseUrl);
# 登录
dr.find_element_by_id("p_username").send_keys("195277")
dr.find_element_by_id("p_password").send_keys("dmb19950522")

dr.find_element_by_id("login_button").click()
sessonCookie = dr.get_cookie("SESSION")
baseUrl = "http://bsms.baogang.info/kp/web/KSCP0101"
dr.get(baseUrl)
for config in MACHINE_CONFIG:
    # requests.post()
    startTime = time.strftime("%Y-%m-%d", time.localtime())
    endtime = time.localtime(time.time() + 60 * 60 * 24 * 90)
    endtime = time.strftime("%Y-%m-%d", endtime)
    uid = uuid.uuid4()
    print("s:{},e:{}".format(startTime, endtime))
    baseUrl="http://bsms.baogang.info/kp/web/KSCP0101"

    dr.get(baseUrl)
    time.sleep(2)


    dr.find_element_by_css_selector("#icon_main-0-templateName").click()
    dr.find_element_by_css_selector("#iframe_popup_KSCP0102 > div.row > iframe")
    #进入iframe
    m = dr.find_element_by_css_selector("#iframe_popup_KSCP0102 > div.row > iframe")
    dr.switch_to.frame(m)
    dr.find_element_by_css_selector("#QUERY").click()
    time.sleep(3)
    dr.find_element_by_css_selector("#ef_grid_result > div.k-grid-content.k-auto-scrollable > table > tbody > tr:nth-child(1)").click()
    time.sleep(1)
    # 离开iframe
    dr.switch_to.default_content()
    time.sleep(1)
    dr.find_element_by_css_selector("#iframe_popup_KSCP0102_fillback").click()


    #填充内容
    dr.find_element_by_css_selector("#main > div.block-content.form-horizontal > div:nth-child(1) > div:nth-child(1) > div > div > span > span > span.k-select > span").click()
    time.sleep(0.1)
    dr.find_element_by_css_selector("#main-0-projectOrContract_listbox > li:nth-child(2)").click()
    dr.find_element_by_css_selector("#main-0-projectName").send_keys("projectName")
    dr.find_element_by_css_selector("#main-0-customeCompany").send_keys("customeCompany")
    dr.find_element_by_css_selector("#main-0-remark5").send_keys("1")
    dr.find_element_by_css_selector("#main-0-remark4").send_keys("remark4")
    dr.find_element_by_css_selector("#main-0-customeDept").send_keys("customeDept")
    dr.find_element_by_css_selector("#main-0-customeEmail").send_keys("customeEmail")
    dr.find_element_by_css_selector("#main-0-createrContact").send_keys("createrContact")
    dr.find_element_by_css_selector("#slave-0-AuthorizedProject").send_keys("AuthorizedProject")
    dr.find_element_by_css_selector("#slave-0-AuthorizedClient").send_keys("AuthorizedClient")
    dr.find_element_by_css_selector("#slave-0-VersionNumber").send_keys("3.6.1")
    dr.find_element_by_css_selector("#slave-0-StartDate").send_keys(startTime)
    dr.find_element_by_css_selector("#slave-0-ExpiringDate").send_keys(endtime)

    dr.find_element_by_css_selector("#slave-0-VCODE").send_keys("1")
    dr.find_element_by_css_selector("#slave-0-TagNumber").send_keys("999999")
    dr.find_element_by_css_selector("#slave-0-ClientNumber").send_keys("500")
    #选择系统和Mac地址
    Select(dr.find_element_by_css_selector("#slave-0-AuthorizedOS")).select_by_value(config["OPERATION"])
    dr.find_element_by_css_selector("#slave-0-HostMAC").send_keys(config["MAC_ADDRESS"])

    dr.find_element_by_css_selector("#F1").click()
    time.sleep(2)
    dr.find_element_by_css_selector("#F2").click()
    time.sleep(2)
    dr.find_element_by_css_selector("#F4").click()
#     requestBody = {"__version__": "2.0",
#                    "__sys__": {"name": "", "descName": "", "msg": "", "msgKey": "", "detailMsg": "", "status": 0,
#                                "traceId": ""},
#                    "__blocks__": {"main": {"attr": {}, "meta": {"desc": "", "attr": {},
#                                                                 "columns": [{"pos": 0, "name": "projectOrContract"},
#                                                                             {"pos": 1, "name": "projectName"},
#                                                                             {"pos": 2, "name": "templateDeptName"},
#                                                                             {"pos": 3, "name": "templateName"},
#                                                                             {"pos": 4, "name": "templateCode"},
#                                                                             {"pos": 5, "name": "rows"},
#                                                                             {"pos": 6, "name": "columns"},
#                                                                             {"pos": 7, "name": "remark5"},
#                                                                             {"pos": 8, "name": "customeCompany"},
#                                                                             {"pos": 9, "name": "remark4"},
#                                                                             {"pos": 10, "name": "customeDept"},
#                                                                             {"pos": 11, "name": "customeEmail"},
#                                                                             {"pos": 12, "name": "customePosition"},
#                                                                             {"pos": 13, "name": "remark2"},
#                                                                             {"pos": 14, "name": "remark1"},
#                                                                             {"pos": 15, "name": "customeContact"},
#                                                                             {"pos": 16, "name": "recCreatorName"},
#                                                                             {"pos": 17, "name": "createrDept"},
#                                                                             {"pos": 18, "name": "createrContact"},
#                                                                             {"pos": 19, "name": "rowguid"},
#                                                                             {"pos": 20, "name": "projectNo"},
#                                                                             {"pos": 21, "name": "remark3"},
#                                                                             {"pos": 22, "name": "approveStatus"},
#                                                                             {"pos": 23, "name": "remark"},
#                                                                             {"pos": 24, "name": "submitor"},
#                                                                             {"pos": 25, "name": "submitorName"},
#                                                                             {"pos": 26, "name": "submitTime"},
#                                                                             {"pos": 27, "name": "approver"},
#                                                                             {"pos": 28, "name": "approverName"},
#                                                                             {"pos": 29, "name": "approveTime"},
#                                                                             {"pos": 30, "name": "approveRemark"},
#                                                                             {"pos": 31, "name": "wfId"},
#                                                                             {"pos": 32, "name": "status"},
#                                                                             {"pos": 33, "name": "licenseNo"},
#                                                                             {"pos": 34, "name": "createrDeptId"},
#                                                                             {"pos": 35, "name": "remark6"},
#                                                                             {"pos": 36, "name": "remark7"},
#                                                                             {"pos": 37, "name": "remark8"},
#                                                                             {"pos": 38, "name": "remark9"},
#                                                                             {"pos": 39, "name": "remark10"},
#                                                                             {"pos": 40, "name": "remark11"},
#                                                                             {"pos": 41, "name": "remark12"},
#                                                                             {"pos": 42, "name": "recCreateTime"},
#                                                                             {"pos": 43, "name": "recCreator"},
#                                                                             {"pos": 44, "name": "archiveFlag"},
#                                                                             {"pos": 45, "name": "archiveStampNo"},
#                                                                             {"pos": 46, "name": "archiveTime"},
#                                                                             {"pos": 47, "name": "companyCode"},
#                                                                             {"pos": 48, "name": "accountSet"}]},
#                                            "rows": [
#                                                ["tmp", "1", "", "iHyperDB-202002",
#                                                 "1f88b6c7-526d-4b91-9919-15cfb71aa35e", "", "", "1", "1", "1", "1",
#                                                 "195277@baosight.com", "", "宝信企业高性能实时数据库软件", "iHyperDB", "", "", "",
#                                                 "18362905003", "", "", "", " ", "", "", "",
#                                                 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
#                                                 "", "", "", "", ""]]},
#                                   "slave": {"attr": {}, "meta": {"desc": "", "attr": {},
#                                                                  "columns": [{"pos": 0, "name": "AuthorizedProject"},
#                                                                              {"pos": 1, "name": "AuthorizedClient"},
#                                                                              {"pos": 2, "name": "VersionNumber"},
#                                                                              {"pos": 3, "name": "StartDate"},
#                                                                              {"pos": 4, "name": "ExpiringDate"},
#                                                                              {"pos": 5, "name": "HostMAC"},
#                                                                              {"pos": 6, "name": "VCODE"},
#                                                                              {"pos": 7, "name": "TagNumber"},
#                                                                              {"pos": 8, "name": "ClientNumber"},
#                                                                              {"pos": 9, "name": "AuthorizedOS"},
#                                                                              {"pos": 10, "name": "LicVersion"},
#                                                                              {"pos": 11, "name": "LicenseMode"},
#                                                                              {"pos": 12, "name": "AuthorizingCompany"},
#                                                                              {"pos": 13, "name": "SupportSPC"},
#                                                                              {"pos": 14, "name": "SupportZT"},
#                                                                              {"pos": 15, "name": "note"}]}, "rows": [
#                                       ["1", "1", "3.6.1", "2021-09-02", "2021-12-02", "00:50:56:98:cd:e3", "", "9999",
#                                        "200", "LINUX",
#                                        "5.0", "CheckHost", "上海宝信软件股份有限公司", "1", "1", ""]]}}}
#     requestBody["__blocks__"]["main"]["rows"][0][4]=uid;
#     requestBody["__blocks__"]["slave"]["rows"][0][3] = startTime;
#     requestBody["__blocks__"]["slave"]["rows"][0][4] = endtime;
#
# js = """var xmlhttp=new XMLHttpRequest();
#         xmlhttp.open("POST","http://127.0.0.1/get.php",false);
#         xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
#         xmlhttp.setRequestHeader("User-Agent","Mozilla/5.0");
#         xmlhttp.setRequestHeader("Cookie","");
#         xmlhttp.send("test=1");
#         return xmlhttp.responseText;
# 	    """
# dr.implicitly_wait(30)
# # time.sleep(30)
# resp = dr.execute_script(js)
